package ch.unisi.inf.pfii.teamwhite.risk.graphic.gameFrame;

/**
 * 
 * @author Matteo Bellan: bellanm@lu.unisi.ch
 * @author Andrea Michelotti: micheloa@lu.unisi.ch
 * @author Ranganathan Ramasubramanian: ramasubr@lu.unisi.ch@lu.unisi.ch
 *
 */

import java.awt.Component;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;

import ch.unisi.inf.pfii.teamwhite.risk.core.Game;
import ch.unisi.inf.pfii.teamwhite.risk.core.Planet;
import ch.unisi.inf.pfii.teamwhite.risk.core.player.Human;

public class PlanetInfoPanel extends JPanel{
	
	private Game game;
	
	private Planet selectedPlanet;

	public PlanetInfoPanel(final Game game, Planet selectedPlanet) {
		this.game = game;
		
		this.selectedPlanet = selectedPlanet;
		
		setLayout(new GridLayout(4, 1));
		setSelectedPlanet();
		if (this.selectedPlanet != null) {
			add(makeDisplayedName());
			add(makeRegion());
			add(makeBuilding());
			add(makeShips());
		} else {
			add(makeNoPlanetSelected(" - No planet selected - "));
			add(makeNoPlanetSelected("Please click on a planet"));
			add(makeNoPlanetSelected("to select it."));
			add(makeNoPlanetSelected(" "));
		}
	}
	
//	Getters and setters
	private void setSelectedPlanet() {
		if (game.getCurrentPlayer().getType().equals("Human")) {
			selectedPlanet = ((Human) game.getCurrentPlayer()).getSelectedPlanet();
		} else {
			selectedPlanet = null;
		}
	}
	
//	This methods creates labels containing the info of a planet to show on the frame
	private Component makeDisplayedName() {
		JLabel displayedNameLabel = new JLabel(selectedPlanet.getDisplayedName());
		return displayedNameLabel;
	}
	
	private Component makeRegion() {
		JLabel nameLabel = new JLabel("This planet is in the " + selectedPlanet.getRegion() + " system");
		return nameLabel;
	}

	private Component makeBuilding() {
		JLabel buildingLabel = new JLabel("This Planet has " + building());
		return buildingLabel;
	}

	private Component makeShips() {
		JLabel shipsLabel = new JLabel("This Planet has " + selectedPlanet.getShips().size() + " ship/s");
		return shipsLabel;
	}
	
	private String building() {
		String result = "a " + selectedPlanet.getBuilding().getName();
		if (selectedPlanet.getBuilding().getName().equals("")) {
			result = "no Buildings";
		}
		return result;
	}
	
//	No selected planet
	private Component makeNoPlanetSelected(String string) {
		JLabel shipsLabel = new JLabel(string);
		return shipsLabel;
	}
}
